package servlet.user;

import bean.dto.UserRoomList;
import bean.pojo.User;
import com.alibaba.fastjson2.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.file.FileUtils;
import commons.format.NumberFormat;
import commons.r.R;
import commons.r.Write;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.bytedeco.libfreenect._freenect_context;
import service.UsersService;
import service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * servlet.user
 * User: 坚持
 * Date: 2024/12/19 10:01
 * motto: 树欲静而风不止
 * Description:
 * Version: V1.0
 */
@MultipartConfig
@WebServlet(name = "UserServlet",value = "/user/CRUD/*")
public class UserServlet extends BaseServlet {

    /**
     * 居民的信息添加
     * @param request
     * @return
     */
    public R add(HttpServletRequest request){
        try {
            BufferedReader bufferedReader = request.getReader();
            String req_data = bufferedReader.readLine();
            System.out.println(req_data);
            User user = JSONObject.parseObject(req_data, User.class);
            UsersService userService = new UsersServiceImpl();
            Boolean add = userService.add(user);
            return add?R.ok():R.error("添加失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("出错");
    }

    /**
     * 根据ID删除居民信息
     * @param request
     * @return
     */
    public R deleteById(HttpServletRequest request){
        Integer userId = NumberFormat.stringToInteger(request.getParameter("userId"));
        UsersService userService = new UsersServiceImpl();
        Boolean delete = userService.deleteById(userId);
        return delete?R.ok():R.error("删除失败");
    }

    /**
     * 修改居民信息
     * @param request
     * @return
     */
    public R update(HttpServletRequest request){
        try {
            BufferedReader bufferedReader = request.getReader();
            String req_data = bufferedReader.readLine();
            User user = JSONObject.parseObject(req_data,User.class);
            UsersService usersService = new UsersServiceImpl();
            Boolean update = usersService.update(user);
            return update?R.ok():R.error("修改失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("出错");
    }

    /**
     * 模糊查询居民信息
     * @param request
     * @return
     */
    public R findBy(HttpServletRequest request){
        Integer page = NumberFormat.stringToInteger(request.getParameter("page"));
        Integer limit = NumberFormat.stringToInteger(request.getParameter("limit"));
        Integer userId = NumberFormat.stringToInteger(request.getParameter("userId"));
        String userName = request.getParameter("userName");
        Integer userAge = NumberFormat.stringToInteger(request.getParameter("userAge"));
        String userSex = request.getParameter("userSex");
        String idCardNumber = request.getParameter("idCardNumber");
        String userPhone = request.getParameter("userPhone");
        Integer isOwner = NumberFormat.stringToInteger(request.getParameter("isOwner"));
        Integer roomId ;
        if (NumberFormat.stringToInteger(request.getParameter("roomId"))==null){
            roomId=0;
        }else {
            roomId=NumberFormat.stringToInteger(request.getParameter("roomId"));
        }
        UsersService usersService = new UsersServiceImpl();

        Long total = usersService.findByTotal(userId,userName,userSex,userAge,idCardNumber,userPhone,isOwner,roomId);

        List<UserRoomList> list=null;
        if(total>0) {
            //当前页码的数据
            list = usersService.userFindBy(page,limit,userId,userName,userSex,userAge,idCardNumber,userPhone,isOwner,roomId);
        }
        return R.ok().addData("total",total).addData("list",list);
    }


    public R  findFacePhoto(HttpServletRequest request){
        Integer userId = NumberFormat.stringToInteger(request.getParameter("userId"));
        UsersService usersService = new UsersServiceImpl();
        User user = usersService.findFacePhoto(userId);
        return R.ok().addData("user",user);
    }
}
